import instacne from "../request";
import type { Ref } from "vue";

// 登录返回数据
interface IAdminLoginParams {
  password: string;
  username: string;
}
interface IAdminLoginRes {
  tokenHead: string;
  token: string;
}

const adminLogin = (data: IAdminLoginParams): Promise<IAdminLoginRes> => {
  // instacne.post("/auth/login", data); 是一个 Promise 类型
  return instacne.post("/admin/login", data);
};

// 获取当前登录用户信息
interface IAdminInfoRes {
  menus: IMneuItem[];
  icon: string;
  username: string;
  roles: string[];
}
const getAdminInfo = (): Promise<IAdminInfoRes> => {
  return instacne.get("/admin/info");
};

// 根据用户名或者姓名获取用户列表
interface IAdminListParams {
  keyword?: string | Ref<string>;
  pageNum?: number | Ref<number>;
  pageSize?: number | Ref<number>;
  parentId?: number | Ref<number>;
}

interface IAdminListItem {
  createTime: string;
  email: string;
  icon: string;
  id: number;
  loginTime: string;
  nickName: string;
  note: string;
  password: string;
  status: number;
  username: string;
}

interface IAdminListRes {
  list: IAdminListItem[];
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

const getAdminList = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("/admin/list", {
    params: data,
  });
};

interface distributed {
  adminid?: string;
  roleIds?: string;
}

//分配角色
const getAdmindistr = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("role/update", {
    params: data,
  });
};
//添加用户
const adminRegister = (data: IAdminListItem): Promise<IAdminListItem> => {
  return instacne.post("/admin/register", data);
};
//显示约束
interface userDisplay {
  id: number;
  status: string;
}
//用户是否启用
const getUserDisplay = (data: userDisplay) => {
  return instacne.post(`/admin/updateStatus/${data.id}?status=${data.status}`);
};
//获取当前用户的角色
// const getRoleAdmin = (data:any) => {
//   return instacne.get("/role/", {
//     params: data,
//   });
// };

// /mall-admin/admin/{id}

//获取资源列表
const getresource = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("resource/list", {
    params: data,
  });
};
//菜单
interface menustreelist {
  id?: string;
  parentId?: number;
  createTime?: string;
  title?: string;
  level?: number;
  sort?: number;
  name?: string;
  icon?: string;
  hidden?: number;
  children?: [];
}
//添加
const addmenus = (data: menustreelist) => {
  return instacne.post("menu/create", data);
};
//编辑菜单
const changemenus = (data: number): Promise<menustreelist> => {
  return instacne.get(`menu/${data}`);
};
//分页菜单列表
const getmenu = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get("menu/list/0", { params: data });
};
//查看下级菜单
const getMenus = (data?: IAdminListParams): Promise<IAdminListRes> => {
  return instacne.get(
    `menu/list/${data?.parentId}?pageNum=${data?.pageNum}&pageSize=${data?.pageSize}`
  );
};
//所有菜单
const getmenus = (data?: number): Promise<menustreelist> => {
  return instacne.get("menu/treeList", { params: data });
};
//菜单显示约束
interface MenuDisplay {
  hidden: string;
  id: number;
}
//菜单显示
const getMenuDisplay = (data: MenuDisplay) => {
  return instacne.post(`/menu/updateHidden/${data.id}?hidden=${data.hidden}`);
};
//用户列表
const getrole = (data: any): Promise<IAdminListRes> => {
  return instacne.get("role/list", { params: data });
};
//添加用户接口
interface Role {
  adminCount?: 0;
  createTime?: string;
  description?: string;
  id?: 0;
  name?: string;
  sort?: 0;
  status?: 1;
}
//添加用户
const createrole = (data: any): Promise<Role> => {
  return instacne.post("role/create", { params: data });
};

//编辑角色
// const changerole=(data:any)=>{
//   return instacne.get(`role/listResource/${data}`);
// }
//分配角色
const distrrole = (id?: string): Promise<menustreelist> => {
  return instacne.get(`role/listMenu/${id}`);
};
//角色是否启用
const getRoleEnable = (data: userDisplay) => {
  return instacne.post(`/role/updateStatus/${data.id}?status=${data.status}`);
};
//分配资源约束
interface resources {
  id: number;
  createTime: string;
  name: string;
  url: string;
  description: string;
  categoryId: number;
}

//分配资源
const getresourcesRole = (data: string): Promise<resources> => {
  return instacne.get(`role/listResource/${data}`);
};

//全部资源
const getresources = (): Promise<resources> => {
  return instacne.get("resource/listAll");
};

//全部资源
const queryCouponData = (): Promise<any> => {
  return instacne.get("resource/listAll");
};

//首页
// 获取index首页的 图表数据接口
const GetIndexDataApi = (): Promise<IIndexStat> => {
  return instacne.get("http://kumanxuan1.f3322.net:8360/admin/stat");
};
export {
  adminLogin,
  getAdminInfo,
  getAdminList,
  getresource,
  getmenu,
  getrole,
  adminRegister,
  addmenus,
  changemenus,
  createrole,
  // changerole,
  getmenus,
  distrrole,
  getresourcesRole,
  getresources,
  GetIndexDataApi,
  getUserDisplay,
  getRoleEnable,
  getMenuDisplay,
  getMenus,
  queryCouponData,
};
export type {
  IAdminLoginParams,
  IAdminLoginRes,
  IAdminListItem,
  menustreelist,
  Role,
};
